package cz.proks.easyhelpdesk.gwt.ui;

import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import cz.proks.easyhelpdesk.api.model.Helpdesk;
import cz.proks.easyhelpdesk.gwt.ui.composite.RestrictedComposite;
import cz.proks.easyhelpdesk.gwt.ui.layout.MainLayoutPanel;
import cz.proks.easyhelpdesk.gwt.ui.widget.RedHyperlinkButton;

public class HelpdesksViewImpl extends RestrictedComposite implements HelpdesksView {

    private static HelpdesksViewImplUiBinder uiBinder = GWT.create(HelpdesksViewImplUiBinder.class);        
        
    @UiField MainLayoutPanel mainLayoutPanel;
    @UiField FlowPanel helpdeskList;
    @UiField Label emptyMessage;
    @UiField RedHyperlinkButton createButton;
        
    private Presenter presenter;    

    interface HelpdesksViewImplUiBinder extends UiBinder<Widget, HelpdesksViewImpl> {
    }    

    public HelpdesksViewImpl() {
        initWidget(uiBinder.createAndBindUi(this));  
        
    }
    
    public MainLayoutPanel getRootPanel() {
        return mainLayoutPanel;
    }
    
    @Override
    public void setPresenter(Presenter presenter) {
        this.presenter = presenter;  
    }         
    
    public void setHelpdesks(Map<Helpdesk, String> helpdeskMap) {
        helpdeskList.clear();
        
        if (helpdeskMap != null)
            for (Helpdesk helpdesk : helpdeskMap.keySet()) {
                Hyperlink helpdeskLink = new Hyperlink(helpdesk.getName(), helpdeskMap.get(helpdesk));
                helpdeskList.add(helpdeskLink);
            }
        
        showHelpdesks(helpdeskList.getWidgetCount() > 0);
    }
    
    @Override
    public void setCreateHelpdeskButtonTargetToken(String targetToken) {
        createButton.setTargetHistoryToken(targetToken);
    }
    
    private void showHelpdesks(boolean show) {
        emptyMessage.setVisible(!show);
        helpdeskList.setVisible(show);
    }                  
}
